﻿using Jinndev.Json;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;

namespace Jinndev {

    public class Setting {

        private static Setting instance;

        public static Setting Instance {
            get {
                if (instance == null) {
                    Reload();
                }
                return instance;
            }
        }

        public static string Path => StorageUtill.GetStoragePath("setting.json");

        public static void Reload() {
            if (StorageUtill.Exists(Path)) {
                string text = StorageUtill.ReadText(Path, Encoding.UTF8);
                instance = JsonUtil.Deserialize<Setting>(text);
            }
            else {
                instance = new Setting();
            }
        }

        public static void Save() {
            if (instance != null) {
                string text = JsonUtil.Serialize(instance, JsonUtil.Pretty);
                StorageUtill.WriteText(Path, text, Encoding.UTF8);
            }
            else {
                Debug.Log("Instance is null");
            }
        }

        public string tinyPngApiKey;        // TinyPNG的ApiKey
        public string projectPath;          // cocos工程路径
        public List<string> projectPathList = new List<string>();
        public int obfuscatorMethod;        // 混淆方式
        public int compressMethod;          // 压缩方式

        public string mapBgColor;
        public string mapGridColor;         // 地图编辑器-网格颜色
        public string mapTextureRootDir;
        public string mapExportRootDir;

        public string exportTcbDir; // 导出TCB数据的存储目录

        public void PrepareProjectPath() {
            if (projectPathList == null) {
                projectPathList = new List<string>();
            }
            else {
                for (int i = projectPathList.Count - 1; i >= 0; i--) {
                    if (!Directory.Exists(projectPathList[i])) {
                        projectPathList.RemoveAt(i);
                    }
                }
                projectPathList.Sort();
                Save();
            }

            if (projectPathList.Count == 0 && !string.IsNullOrWhiteSpace(projectPath)) {
                projectPathList.Add(projectPath);
            }
        }

    }

}
